package com.freak.wanandroid.model.discover;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.freak.httpmanager.BaseViewModel;
import com.freak.httpmanager.callback.ApiCallback;
import com.freak.httpmanager.callback.SubscriberCallBack;
import com.freak.wanandroid.bean.ArticleListBean;
import com.freak.wanandroid.net.CommonLiveData;

/**
 * @author: freak
 * @date: 2022/9/25
 */
public class DiscoverViewModel extends BaseViewModel<DiscoverRepository> {
    public MutableLiveData<ArticleListBean> mGetArticle = new MutableLiveData<>();
    public MutableLiveData<String> mGetArticleError = new MutableLiveData<>();
    public MutableLiveData<CommonLiveData> mDoCollect = new MutableLiveData<>();
    public MutableLiveData<CommonLiveData> mDoCancelCollect = new MutableLiveData<>();

    public DiscoverViewModel(@NonNull Application application) {
        super(application);
    }

    public void doCollect(int id) {
        mRepository.doCollect(id, new SubscriberCallBack<>(new ApiCallback<String>() {
            @Override
            public void onSuccess(String model) {
                mDoCollect.postValue(new CommonLiveData(CommonLiveData.SUCCESS, model));
            }

            @Override
            public void onFailure(String msg) {
                mDoCollect.postValue(new CommonLiveData(CommonLiveData.ERROR, msg));
            }
        }));
    }

    public void getArticle(String curPage) {
        mRepository.getArticle(curPage, new SubscriberCallBack<>(new ApiCallback<ArticleListBean>() {
            @Override
            public void onSuccess(ArticleListBean model) {
                mGetArticle.postValue(model);
            }

            @Override
            public void onFailure(String msg) {
                mGetArticleError.postValue(msg);
            }
        }));
    }

    public void doCancelCollect(int id) {
        mRepository.doCancelCollect(id, new SubscriberCallBack<>(new ApiCallback<String>() {
            @Override
            public void onSuccess(String model) {
                mDoCancelCollect.postValue(new CommonLiveData(CommonLiveData.SUCCESS, model));
            }

            @Override
            public void onFailure(String msg) {
                mDoCancelCollect.postValue(new CommonLiveData(CommonLiveData.ERROR, msg));
            }
        }));
    }
}
